package cn.signalpha.modules.com.controller.admin;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.utils.Func;
import cn.signalpha.modules.com.entity.UserTag;
import cn.signalpha.modules.com.service.UserTagService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_USER_NAME + "/admin/user/tag")
@Api(value = "用户标签", tags = "用户标签")
public class UserTagController extends BaseController {

	private final UserTagService userTagService;

    @GetMapping("/page")
    @ApiImplicitParams({
        @ApiImplicitParam(name="status", dataType = "string", value = "状态"),
        @ApiImplicitParam(name="name", dataType = "string", value = "名称"),
    })
    @ApiOperation(value = "分页", notes = "")
    public R<IPage<UserTag>> page(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
        QueryWrapper<UserTag> wrapper = userTagService.selectUserTagWrapper(params);
        return R.data(userTagService.page(Condition.getPage(query), wrapper));
    }

    @GetMapping("/list")
    @ApiOperation(value = "列表", notes = "")
    public R<List<UserTag>> list(@ApiIgnore @RequestParam Map<String, Object> params) {
        QueryWrapper<UserTag> wrapper = userTagService.selectUserTagWrapper(params);
        return R.data(userTagService.list(wrapper));
    }

	@GetMapping("/info")
	@ApiOperation(value = "详情", notes = "传入id")
	public R<UserTag> info(@ApiParam(value = "主键", required = true) @RequestParam Long id) {
		return R.data(userTagService.getById(id));
	}

    @PostMapping("/submit")
    @ApiOperation(value = "提交", notes = "传入userTag")
    public R submit(@Valid @RequestBody UserTag userTag) {
		if(userTag.getId() == null){
			userTag.setCreateTime(new Date());
		}
		userTag.setUpdateTime(new Date());
        return R.status(userTagService.saveOrUpdate(userTag));
    }

	@PostMapping("/remove")
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(userTagService.removeByIds(Func.toLongList(ids)));
	}

}
